<?php 
namespace Admin\Controller;
use Think\Controller;

class AdvertController extends AdminController
{
	public function index()
	{
		$data=D('advert')->select();
		$this->assign('list',$data);
		$this->display();	
	}

	public function add()
	{
		$this->display();	
	}

    public function doadd()
    {
    	$advert=D('advert');
    	// var_dump($_FILES);exit;
        if(!empty($_FILES)){
			//上传单个图像
			$upload=new \Think\Upload();
	    	$upload->maxSize=3145678;
	    	$upload->exts=array('jpg','gif','png','jpeg');
	    	$upload->rootPath='Uploads/';
	    	$upload->savePath='';
	    	$upload->saveName='com_create_guid';
	    	$upload->autoSub=true;
	        $upload->subName = array('date','Ymd');
	    	$info=$upload->uploadOne($_FILES['img']);
	    	if (!$info) {
	    		$this->error($upload->getError());
	    	} else {
	    		$filepath = $info['savepath'].$info['savename'];
	    		$data['img']=$filepath;
	    		$data['url']=I('post.url');
	    		$data['status']=I('post.status');
	    		$data['advert']=I('post.advert');
	    		
	    		$advert->create($data);
	    		$result=$advert->add();
		    	    if(!$result){
		    			$this->error('上传失败!');
		    		}else{
			    		$this->success('上传成功!',U('index'));
	    		    }	
		    	}
		}	
    }


    //编辑页
    public function edit($id)
    {
        //接收ID
        $id = I('get.id/d');
        //查找单条数据
        $data = M('advert')->find($id);
        $this->assign('data',$data);
        $this->display('advert/edit');
    }

    //执行修改
    public function update()
    {
        if (empty($_POST)) {
            $this->error('请填写数据!',U('advert/edit'));
            exit;
        }
        $id=I('post.id/d');
        $advert=M('advert');
       
        $path="./Uploads/";
        if(!empty($_FILES)){
			//上传单个图像
			$upload=new \Think\Upload();
	    	$upload->maxSize=3145678;
	    	$upload->exts=array('jpg','gif','png','jpeg');
	    	$upload->rootPath='Uploads/';
	    	$upload->savePath='';
	    	$upload->saveName='com_create_guid';
	    	$upload->autoSub=true;
	        $upload->subName = array('date','Ymd');
	    	$info=$upload->uploadOne($_FILES['img']);
	    	if (!$info) {
	    		$this->error($upload->getError());
	    	} else {
	    		$filepath = $info['savepath'].$info['savename'];
	    		$data['img']=$filepath;
	    		$advert->create();
	    		$result=$advert->where('id='.$id)->save($data);
		    	    if(!$result){
		    			$this->error('上传失败!');
		    		}else{
			    		$this->success('上传成功!',U('index'));
	    		    }	
		    	}
			}	
    }    

    public function del()
    {
        if (empty($_GET['id'])) {
	            $this->redirect('index');
	            exit;
        }
        $id = I('get.id/d');
        $img=M('advert')->where(array('id'=>$id))->find();
        // var_dump($img);exit;
        $path="./Uploads/";
        // var_dump($path);
        unlink($path.$img['img']);
        if (M('advert')->delete($id) > 0) {
            $this->success('恭喜您!删除成功!',U('index'));
        } else {
            $this->error('删除失败!',U('index'));
        }
    }

	public function status()
	{
		$id =  I('get.id/d');
		$data['status'] = $_GET['s'];
		$advert = M('advert');
		$advert->where(array('id'=>$id))->save($data);
        $this->redirect('index');
	}

    
}